package com.hngy.xpq.chatdemo;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;

import com.hngy.xpq.chatdemo.adapter.ChatAdapter;
import com.hngy.xpq.chatdemo.bean.Chat;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    RecyclerView chat;
    Button friend;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // 初始化
        init();
        onclick();
    }

    private void onclick() {
        friend.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, FriendActivity.class);
                startActivity(intent);
            }
        });
    }

    private void init() {
        // 获取屏幕宽度
        WindowManager manager = (WindowManager) this.getSystemService(Context.WINDOW_SERVICE);
        int width = manager.getDefaultDisplay().getWidth();
        // 绑定控件
        chat = findViewById(R.id.chat);
        friend = findViewById(R.id.infoFriend);
        // 聊天信息适配器
        initChatAdapter(getListChat(), width);
    }

    private void initChatAdapter(List<Chat> listChat, int width) {
        StaggeredGridLayoutManager layoutManager = new StaggeredGridLayoutManager(1,StaggeredGridLayoutManager.VERTICAL);
        chat.setLayoutManager(layoutManager);
        ChatAdapter adapter = new ChatAdapter(listChat, width);
        chat.setAdapter(adapter);
    }

    private List<Chat> getListChat() {
        // 获取聊天数据（本地数据获取<推荐> or 服务器数据获取）
        // 图片按道理应该是String -> 存储地址 or Url -> 网络地址，这里使用drawable取代
        List<Chat> list = new ArrayList<>();
        Chat chat1 = new Chat((long) 1001, String.valueOf(R.drawable.d), "学习群", "许...：小卷不算卷", "2023-6-2 9:00");
        list.add(chat1);
        Chat chat2 = new Chat((long) 1002, String.valueOf(R.drawable.a), "Man", "Man：儿子，吃饭没?", "2023-6-2 18:00");
        list.add(chat2);
        Chat chat3 = new Chat((long) 1003, String.valueOf(R.drawable.b), "Dad", "Dad：过的怎么样，没人欺负你吧.有什么事，都和我说.", "2023-6-1 21:00");
        list.add(chat3);
        Chat chat4 = new Chat((long) 1004, String.valueOf(R.drawable.c), "导师", "收到", "2023-5-31 18:10");
        list.add(chat4);
        Chat chat5 = new Chat((long) 1005, String.valueOf(R.drawable.c), "导师2-测试时间计算", "收到", "2023-5-30 18:10");
        list.add(chat5);
        // 获取系统时间
        Calendar ca = Calendar.getInstance();
        int mYear = ca.get(Calendar.YEAR);
        int mMonth = ca.get(Calendar.MONTH) + 1;
        int mDay = ca.get(Calendar.DAY_OF_MONTH);
        // 处理数据
        for (Chat c:list) {
            c.setTime(algTime(mYear, mMonth, mDay, c.getTime()));
        }
        return list;
    }

    /**
     * 时间转换
     * @param mYear 获取系统年
     * @param mMonth 获取系统月
     * @param mDay 获取系统日
     * @param time chat.getTime()
     * @return
     */
    private String algTime(int mYear, int mMonth, int mDay, String time) {
        String temp = "";
        // 切割时间 例：2023-6-2 9:00
        String[] times = time.split(" ");
        // 判断时间正确性
        if (times.length == 2) {
            String[] strings = times[0].split("\\-");
            int year = Integer.valueOf(strings[0]);
            int month = Integer.valueOf(strings[1]);
            int day = Integer.valueOf(strings[2]);
            // 如果日期为每月前两天，则需要欠上一个月
            int d;
            if (mMonth - month == 1 && mDay < 3) {
                d = mDay + algDay(year, month) - day;
            } else {
                d = mDay - day;
            }
            if (year == mYear && mMonth - month < 2  && d < 3) {  // 判断年月
                String[] mHour = times[1].split("\\:");
                switch (d) {
                    case 0:
                        if (Integer.valueOf(mHour[0]) <= 12) {
                            temp += "上午 " + times[1];
                        } else {
                            temp += "下午 " + (Integer.valueOf(mHour[0]) - 12) + ":" + mHour[1];
                        }
                        break;
                    case 1:
                        temp = "昨天 " + times[1];
                        break;
                    case 2:
                        temp = "前天 " + times[1];
                        break;
                    default:
                        break;
                }
            } else {
                temp = times[0];
            }
        }
        return temp;
    }

    /**
     * 计算：当前年月的天数
     * @param year
     * @param month
     * @return
     */
    private int algDay(int year, int month) {
        boolean isRunYear;
        int day = 0;
        // 判断是否为闰年
        if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
            isRunYear = true;
        } else {
            isRunYear = false;
        }
        // 判断月份
        if (month > 0 && month < 13) {
            if (month != 2) {
                switch (month) {
                    case 1:
                    case 3:
                    case 5:
                    case 7:
                    case 8:
                    case 10:
                    case 12:
                        day = 31;
                        break;
                    case 4:
                    case 6:
                    case 9:
                    case 11:
                        day = 30;
                        break;
                    default:
                        break;
                }
            } else {
                if (isRunYear) {
                    day = 29;
                } else {
                    day = 28;
                }
            }
        }
        return day;
    }

}